package com.tanhua.admin.service;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.admin.interceptor.AdminHolder;
import com.tanhua.admin.mapper.AdminMapper;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.Admin;
import com.tanhua.model.vo.AdminVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 用户登录
     *
     * @param map
     * @return java.util.Map
     * @Author Pan Rundong
     */
    public Map login(Map map) {
        // 1.获取Map中的参数
        String username =  map.get("username").toString();
        String password =  map.get("password").toString();
        String verificationCode =  map.get("verificationCode").toString();
        String uuid =  map.get("uuid").toString();

        // 2.判断用户名以及密码是否为空，是则封装错误map并返回
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            Map emptyMap = new HashMap<>();
            emptyMap.put("message", "用户名或者密码为空");
            return emptyMap;
        }

        // 3.判断验证码是否为空，是则返回验证码为空错误
        if (StringUtils.isEmpty(verificationCode)) {
            throw new BusinessException("验证码不能为空");
        }

        // 4.从Redis获取验证码，对照验证码是否一致，不一致则返回错误
        String code = redisTemplate.opsForValue().get(Constants.CAP_CODE + uuid);
        if (StringUtils.isEmpty(code) || !code.equals(verificationCode)) {
            throw new BusinessException("验证码不正确");
        }

        // 5.根据用户名查询用户，并对密码进行md5加密
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        password = SecureUtil.md5(password);

        // 6.比较用户名和密码不同则抛出密码错误
        if (admin == null || !admin.getPassword().equals(password)) {
            throw new BusinessException("用户名或密码不正确");
        }

        // 7.将用户名及密码存入map，并获得对应Token
        Map tokenParam = new HashMap<>(4);
        tokenParam.put("username", username);
        tokenParam.put("id", admin.getId());
        String token = JwtUtils.getToken(tokenParam);

        // 8.返回对应token的map
        Map tokenMap = new HashMap<>(2);
        tokenMap.put("token", token);
        return tokenMap;
    }

    /**
     * 获取用户资料
     *
     * @param
     * @return com.tanhua.model.vo.AdminVo
     * @Author Pan Rundong
     */
    public AdminVo profile() {
        // 1.获取用户Id
        Long userId = AdminHolder.getUserId();

        // 2.根据用户Id获取admin信息
        Admin admin = adminMapper.selectById(userId);

        // 3.返回参数
        return AdminVo.init(admin);
    }
}
